<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="学生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
              <j-dict-select-tag type="list"  v-model="model.studentId" :dictCode="`sk_student,name,id,del_flag=0`" placeholder="请选择学生" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveType">
              <j-dict-select-tag type="list" v-model="model.leaveType" dictCode="leave_type" placeholder="请选择请假类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择请假开始时间" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" v-model="model.startTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择请假结束时间" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="请假时长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="length">
              <a-input-number v-model="model.length" placeholder="请输入请假时长" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="请假事由" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveExplain">
              <a-textarea v-model="model.leaveExplain" rows="4" placeholder="请输入请假事由" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalState">
              <j-dict-select-tag type="list" v-model="model.approvalState" dictCode="approval_state" placeholder="请输入审批状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvar">
              <a-input v-model="model.approvar_dictText" placeholder="请输入审批人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <div v-if="disabled && model.approvalState != 1">
            <a-col :span="24" >
              <a-form-model-item label="审批时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalTime">
                <a-input v-model="model.approvalTime" ></a-input>
              </a-form-model-item>
            </a-col>
             <a-col :span="24">
              <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
                <a-input v-model="transformApprovalResult" placeholder="请输入审批结果" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
                <a-textarea v-model="model.approvalInfo" rows="4" placeholder="请输入审批信息" />
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-form-model :model="model" v-if="approvlStatus && !disabled ">
      <a-col :span="24">
          <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
            <j-dict-select-tag type="list" v-model="model.approvalResult" dictCode="approval_result" placeholder="请输入审批状态" />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
            <a-textarea v-model="model.approvalInfo" rows="4" placeholder="请输入审批信息" />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="审批时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalTime">
            <j-date placeholder="请选择审批时间" v-model="model.approvalTime"  style="width: 100%" />
          </a-form-model-item>
        </a-col>
    </a-form-model>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { filterDictTextByCache} from '@/components/dict/JDictSelectUtil'
  export default {
    name: 'SkStudentLeaveForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/management/skStudentLeave/add",
          edit: "/management/skStudentLeave/edit",
          queryById: "/management/skStudentLeave/queryById"
        },
        approvlStatus:null
      }
    },
    computed: {
      formDisabled(){
        return this.approvlStatus
      },
      filterDictTextByCacheText(){
        return filterDictTextByCache("approval_state",this.model.approvalState)
      },
      transformApprovalResult(){
        return filterDictTextByCache("approval_result",this.model.approvalResult)
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record,approvlStatus) {
        this.model = Object.assign({}, record);
        this.approvlStatus = approvlStatus;
        if (this.disabled) {
          this.approvlStatus = true;
        }
        this.visible = true;
      },
      approvl (record,approvlStatus) {
        this.model = Object.assign({}, record);
        this.approvlStatus = approvlStatus;
        if (this.disabled) {
          this.approvlStatus = true;
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            Reflect.deleteProperty(this.model,'classId');
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>
<style lang="less" scoped>
.ant-select-disabled{
  color:rgba(0, 0, 0, 0.65);
  /deep/ .ant-select-selection{
    background: #fff ;
  }
}
 
</style>